package logic

import (
	"context"
	"errors"
	"house_srv/model/mysql"

	"house_srv/house"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseAddLogic {
	return &HouseAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseAddLogic) HouseAdd(in *house.HouseAddRequest) (*house.HouseAddResponse, error) {
	// todo: add your logic here and delete this line
	var houses mysql.House
	mysql.DB.Debug().Where("name=?", in.Name).Limit(1).Find(&houses)
	if houses.ID != 0 {
		return nil, errors.New("该房源已发布")
	}
	m := mysql.House{
		Name:    in.Name,
		Area:    float64(in.Area),
		Price:   float64(in.Price),
		Address: in.Address,
	}
	mysql.DB.Debug().Create(&m)
	return &house.HouseAddResponse{Id: int64(m.ID)}, nil
}
